package com.woniuxy.project115.kaer.platform.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.project115.kaer.commons.util.JwtUtil;
import com.woniuxy.project115.kaer.commons.util.Result;
import com.woniuxy.project115.kaer.platform.Param.ApproveForm;
import com.woniuxy.project115.kaer.platform.Param.ApproveParam;
import com.woniuxy.project115.kaer.platform.annotation.RequirePermission;
import com.woniuxy.project115.kaer.platform.annotation.RequireRole;
import com.woniuxy.project115.kaer.platform.dto.LoginDto;
import com.woniuxy.project115.kaer.platform.service.AccountService;
import com.woniuxy.project115.kaer.platform.util.RedisKey;
import com.woniuxy.project115.kaer.tenant.feign.TenantFeignClient;
import com.woniuxy.project115.kaer.tenant.form.ApproveApplicationForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fyy
 * @since 2025年02月23日
 */
@RestController
@RequestMapping("/account")
@Api(tags = "平台管理")
public class AccountController {
    @Resource
    private AccountService accountService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private TenantFeignClient tenantFeignClient;

    //超级管理员新增管理员接口
    @PostMapping("/addAdmin")
    @ApiOperation("超级管理员新增管理员接口")
    @RequireRole("超级管理员")
    @RequirePermission("新增管理员")
    public Result addAdmin(String userName, String passWord) throws Exception {
        accountService.addAdmin(userName,passWord);
        return Result.succeed("新增管理员成功",null);
    }

    //管理员登录接口
    @PostMapping("/login")
    @ApiOperation("管理员登录接口")
    public Result login(String username, String password, HttpServletResponse response, HttpServletRequest req) throws Exception {
        LoginDto loginDto =accountService.login(username,password,req);
        //生成jwt token并返回
        final Map<String, Object> stringObjectMap = BeanUtil.beanToMap(loginDto);
        //生成短期token
        final String accessToken = JwtUtil.createToken(stringObjectMap,2*24*60);
        //生成长期token
        final String refreshToken = JwtUtil.createToken(stringObjectMap, 3 * 24 * 60);
        //通过响应头响应给前端
        //通过响应头将jwt响应给客户端
        response.setHeader("token",accessToken);
        //将短期token存入redis
        stringRedisTemplate.opsForValue().set(RedisKey.ACCESS_EXPIRE(accessToken),"",30,TimeUnit.MINUTES);
        //将长期token存入redis
        stringRedisTemplate.opsForValue().set(RedisKey.ACCESS_REFRESH(accessToken),refreshToken,3*24*60,TimeUnit.HOURS);
        //在跨域请求访问时 必须要公开请求头 前端才能获取列
        response.setHeader("Access-Control-Expose-Headers","token");
        return Result.succeed("登录成功",loginDto);
    }

    //管理员同意商家申请接口
    @PostMapping("/agreeApply")
    @ApiOperation("管理员同意商家申请接口")
    @RequireRole("普通管理员")
    @RequirePermission("查询所有商家待处理申请")
    public Result agreeApply(@RequestBody ApproveForm approveForm) throws Exception {
        accountService.agreeApply(BeanUtil.toBean(approveForm, ApproveParam.class));
        return Result.succeed("查询商家申请成功",null);
    }

    //拒绝商家申请接口
    @PostMapping("/refuseApply")
    @ApiOperation("拒绝商家申请接口")
    @RequireRole("普通管理员")
    @RequirePermission("拒绝商家申请")
    public Result refuseApply(@RequestBody ApproveForm approveForm) throws Exception {
        tenantFeignClient.approveApplication(BeanUtil.toBean(approveForm, ApproveApplicationForm.class));
        return Result.succeed("商家申请失败",null);
    }

    //根据申请表id查询附件接口
    @PostMapping("/getAttachmentById")
    @ApiOperation("根据申请表id查询附件接口")
    @RequireRole("普通管理员")
    @RequirePermission("根据申请表查询申请附件")
    public Result getAttachmentById(Integer applicationId) throws Exception {
        return Result.succeed("查询附件成功",tenantFeignClient.getAttachmentById(applicationId));
    }

    //查询所有商家申请待处理接口
    @PostMapping("/queryApply")
    @ApiOperation("查询所有商家申请待处理接口")
    @RequireRole("普通管理员")
    @RequirePermission("查询所有商家待处理申请")
    public Result queryApply() throws Exception {
        return Result.succeed("查询商家申请成功",accountService.queryApply());
    }

    //查询所有管理员接口
    @GetMapping("/getAllAdmin")
    @ApiOperation("查询所有管理员接口")
    @RequireRole("超级管理员")
    @RequirePermission("查询所有管理员")
    public Result getAllAdmin() throws Exception {
        return Result.succeed("查询管理员成功",accountService.getAllAdmin());
    }

    //删除管理员接口
    @DeleteMapping("/deleteAdmin/{id}")
    @ApiOperation("删除管理员接口")
    @RequireRole("超级管理员")
    @RequirePermission("删除管理员")
    public Result deleteAdmin(@PathVariable String id) throws Exception {
        accountService.deleteAdmin(id);
        return Result.succeed("删除管理员成功",null);
    }

}

